<template>        
    <Tree :data="trees" slot="trees" :load-data="treeData" @on-select-change="getNodes"></Tree>
</template>
<script>   
  export default {
    props: {
        type: {
            type: [Boolean,String],
            default: false
        },
    },
    data () {
      return {
			
            // 菜单树
            trees: [],
            treesHead: {
              title: '年级菜单',
              button: false,
              button_text: '添加按钮'
            },
            jieUrl: 'api/student/arrives/getAllList?period_id=',
            periodUrl: null,
            eclassUrl: null,
        }
    },
    methods: {

        /************************************************************
         * 
         * 请求学段数据
         * 
         ************************************************************/
        load() {
            this.$http.get(this.periodUrl)
            .then(res=>{  
                if(this.type==false){
                    res.data.forEach(el => {
                        this.trees.push({
                            title: el.period_name.zh?el.period_name.zh:el.period_name.mn,
                            value: el.id,
                            loading: false,
                            state: 1,
                            type : 'period',
                            type_id : el.id,
                            children: []
                        });
                    }); 
                }  
                if(this.type=='grade'){
                    res.data.forEach(el => {
                        this.trees.push({
                            title: el.grade_name.zh?el.grade_name.zh:el.grade_name.mn,
                            value: el.id,
                            loading: false,
                            state: 2,
                            type : 'grade',
                            type_id : el.id,
                            children: []
                        });
                    }); 
                }           
            })
            .catch(err=>{
                console.log(err);
                if(err.code && err.code==422) {
                    this.$Message.error(err.message);
                }
            })
        },
        
        /************************************************************
         * 
         * 树子级
         * 
         ************************************************************/
        treeData(item, callback) {
            if(item.state==1){
                this.$http.get(this.jieUrl + item.value)
                .then(
                    val => {
                        let arr = [];
                        val.data.forEach(el => {
                            let names = ''
                            let name1 = el.grade
                            let name2 = el.grade_id
                            if (name1 == null && name2 == null) {
                                names = '(未入学)'
                            }
                            if (name1 == null && name2 == 0) {
                                names = '(已毕业)'
                            }
                            if (name1 != null && name2 != null) {
                                names = '(' + name1.grade_name.zh?name1.grade_name.zh:name1.grade_name.mn + ')'
                            }
                            arr.push({
                                // title: el.name ? el.name.zh : el.name.mn + names,
                                title: el.name ? el.name.zh : names,
                                value: el.id,
                                type: 'grade',
                                type_id: el.grade_id,
                                state: 2,
                                loading: false,
                                children: []
                            });
                        });
                        callback(arr);
                    })
                .catch(err=>{
                    console.log(err);
                    if(err.code && err.code==422) {
                        this.$Message.error(err.message);
                    }
                })
            }
            if(item.state==2 && this.type==false){
                this.$http.get(this.eclassUrl + item.value)
                .then(val => {
                    console.log(val, '0999')
                    let arr = val.data
                    // arr.forEach((item1)=>{
                    //     item1.grade_ids=item.type_id
                    // })
                    callback(this.process(arr))
                })
                .catch(err=>{
                    console.log(err);
                    if(err.code && err.code==422) {
                        this.$Message.error(err.message);
                    }
                })
            }
            if(item.state==2 && this.type=='grade'){
                this.$http.get(this.eclassUrl + item.value)
                .then(val => { 
                    callback(this.process(val.data))
                })
                .catch(err=>{
                    console.log(err);
                    if(err.code && err.code==422) {
                        this.$Message.error(err.message);
                    }
                })
            }
        },
            
        /************************************************************
         * 
         * 点击树
         * 
         ************************************************************/
        getNodes(data) {
            if(data.length != 0){
                if(data[0].type=='grade' && data[0].type_id==0){
                    this.$Message.error('请选择班级')
                    return
                }
                let val = {
                    type:data[0].type,
                    type_id:data[0].type_id,
                    grade_ids:data[0].grade_ids
                }
                this.$emit('on-treeselect',val) 
            }
        },
            
        /************************************************************
         * 
         * 处理数据
         * 
         ************************************************************/
        process(data) {
            let arr = []
            data.forEach(item => {
                if(item.eclass_name === undefined){
                    item.title = item.grade_name ? item.grade_name.zh : ''
                    item.loading = false,
                    item.children = []
                } else {
                    item.title = item.eclass_name ? item.eclass_name.zh : item.eclass_name.mn
                }
                item.type = 'eclass',
                item.type_id = item.id,
                item.value = item.id
                arr.push(item)
            })
            return arr
        },
    },
    mounted(){
        if(this.type==false){
            this.periodUrl = 'api/systemctl/schoolsettings/get/myschool/periods'
            this.eclassUrl =  'api/student/eclass/getSchoolByEclass?arrives_id='
        }
        if(this.type=='grade'){
            this.periodUrl = 'api/student/v1/studentlearnstatus/my_grades'
            this.eclassUrl = 'api/eclass/v1/eclass/grade_eclass?grade_id='
        }
        this.load()
    }
}
</script>
